﻿using SMARTMobile.Common;
using SMARTMobile.Models;
using SMARTMobile.Models.UI;
using SMARTMobile.Services.SMARTMobileServiceReference;
using SMARTMobile.WinPhone;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace SMARTMobile.ViewModels
{
    public partial class NoTelerikMainViewModel : ViewModelBase
    {
        private readonly ReloadDataSmartPhoneSoapClient _proxy;

        private ARSaleSPTypeModel _registeredUser = ARSaleSPTypeModel.CreateTestingDsr();

        public NoTelerikMainViewModel()
        {
            _proxy = new ReloadDataSmartPhoneSoapClient(GlobalInformation.Binding, GlobalInformation.Endpoint);

            this.ContentPanelLoadedHandlerCommand = new CommandRoute
            {
                ExecuteHandler = this.OnContentPanelLoaded
            };

            this.MenubarLoadedHandlerCommand = new CommandRoute
            {
                ExecuteHandler = this.OnMenubarLoaded
            };

            this.CloseSidebarCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteCloseSidebarCommand
            };

            this.QuyTrinhCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteQuyTrinhCommand
            };

            this.KhachHangCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteKhachHangCommand
            };

            this.TacVuCommand = new CommandRoute
            {
                ExecuteHandler = this.ExecuteTacVuCommand
            };
        }

        public event Action<bool?> SidebarStateChanged;

        #region Commands

        public ICommandRoute ContentPanelLoadedHandlerCommand { get; private set; }

        public ICommandRoute MenubarLoadedHandlerCommand { get; private set; }

        public ICommandRoute CloseSidebarCommand { get; private set; }

        public ICommandRoute QuyTrinhCommand { get; private set; }

        public ICommandRoute KhachHangCommand { get; private set; }

        public ICommandRoute TacVuCommand { get; private set; }

        #endregion

        #region Properties

        public ARSaleSPTypeModel RegisteredUser
        {
            get { return this._registeredUser; }
            set
            {
                this._registeredUser = value;
                base.RaisePropertyChanged("RegisteredUser");
            }
        }

        #endregion

        #region ViewModelBase Overridens

        public override void PageLoaded(Uri uri)
        {
            this._proxy.GetuserTypeCompleted += Proxy_GetUserTypeCompleted;
            this._proxy.GetGroupMenuCompleted += Proxy_GetGroupMenuCompleted;
            this._proxy.GetMenuCompleted += Proxy_GetMenuCompleted;
        }

        public override void Initialize(Uri uri)
        {
            this._proxy.GetuserTypeAsync("Nguyen Thanh Phu", "64ff3f81e1f8eb08", "00", "DSR003");
            this._proxy.GetGroupMenuAsync();
            this._proxy.GetMenuAsync();

            this.LoadMenuData();
        }

        public override void PageUnloaded(Uri uri)
        {
            this._proxy.GetuserTypeCompleted -= Proxy_GetUserTypeCompleted;
            this._proxy.GetGroupMenuCompleted -= Proxy_GetGroupMenuCompleted;
            this._proxy.GetMenuCompleted -= Proxy_GetMenuCompleted;
        }

        #endregion

        partial void LoadMenuData();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="userType">DSR or SS</param>
        partial void PopulateActionButtonPanel(string userType, string appStatus);

        #region Command Action Handlers

        private void ExecuteCloseSidebarCommand(object commandParams)
        {
            this.CloseSidebarCommand.RaiseCanExecuteChanged(true);
            this.OnSidebarStateChanged(null);
        }

        private void ExecuteQuyTrinhCommand(object commandParams)
        {
            this.QuyTrinhCommand.RaiseCanExecuteChanged(true);
            this.OnSidebarStateChanged(true);
        }

        private void ExecuteKhachHangCommand(object commandParams)
        {
            this.KhachHangCommand.RaiseCanExecuteChanged(true);
            this.OnSidebarStateChanged(true);
        }

        private void ExecuteTacVuCommand(object commandParams)
        {
            this.TacVuCommand.RaiseCanExecuteChanged(true);
            this.OnSidebarStateChanged(true);
        }

        #endregion

        #region Web-service Event Handlers

        private void Proxy_GetUserTypeCompleted(object sender, GetuserTypeCompletedEventArgs e)
        {
            // return 1 - WTF
        }

        private void Proxy_GetMenuCompleted(object sender, GetMenuCompletedEventArgs e)
        {
            ArrayOfXElement result = e.Result;

            List<XmlMenu> menuList = XmlHelper.GetItems<XmlMenu>(e.Result.Nodes[1], "Table").ToList();
        }

        private void Proxy_GetGroupMenuCompleted(object sender, GetGroupMenuCompletedEventArgs e)
        {
            ArrayOfXElement result = e.Result;

            List<XmlGroupMenu> menuGroupList = XmlHelper.GetItems<XmlGroupMenu>(e.Result.Nodes[1], "Table").ToList();
        }

        #endregion

        private void OnSidebarStateChanged(bool? sidebarState)
        {
            if (this.SidebarStateChanged != null)
                this.SidebarStateChanged(sidebarState);
        }
    }
}
